<?php
require_once('version.php');
require_once('functions.php');
require_once('config.php');

session_start();

if($_GET['logout'] == 1)
{
	session_unset();
}

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	//highlight checkbox
	$app_highlight = $_POST['app_highlight'];
	if($app_highlight)
		$app_checked = 'checked="checked"';
	
	//output mode radio
	$app_output_mode = $_POST['app_mode'];
	$app_omc = ($app_output_mode == 'code') ? 'checked="checked"' : '';
	$app_omh = ($app_output_mode == 'html') ? 'checked="checked"' : '';
	
			
	if($_SESSION['app_status'] == 'valid')
	{
		global $app_error;
		$app_error = array();
		
		$app_code = stripslashes($_POST['app_code']);
		
		
		ob_start();		
		$app_output = app_eval($app_code);
		$app_parse_err = strip_tags(ob_get_clean());

		/* It is not possible to catch parse errors in
		eval()'d code using an error handler. One solution
		is to parse out the error message from the output */
		if($app_parse_err != '')
		{
			if( preg_match( '/Parse error:\s*syntax error,(.+?)\s+in\s+.+?\s*line\s+(\d+)/', $app_parse_err, $match ) )
			{
				$app_error[] = array(
					'type'		=>	'Parse Error',
					'line'		=>	$match[2],
					'message'	=>	$match[1]
					);
			}
		}

	}
	else
	{
		$app_pass = $_POST['app_pass'];
		if($app_pass == APP_PASSWORD)
			$_SESSION['app_valid'] = 1;
		
	}
}

if($_SESSION['app_valid'] != 1) :
	$_SESSION['app_status'] = 'login';
	include(APP_TEMPLATE . '/login.php');

else :

	$_SESSION['app_status'] = 'valid';
	include(APP_TEMPLATE . '/header.php');
	$app_elements = app_get_elements();
	
	foreach($app_elements as $e)
	{
		include(APP_TEMPLATE . '/' . $e);
		echo '<br />';
	}
	include(APP_TEMPLATE . '/footer.php');
	
endif; 
?>